/**
 * 
 */
package info.niwota.profile.contact;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
/**
 * @author qiangli
 * 
 */
public final class ContactUtils {

	public static void loadAddresses(Context ctx, List<EmailAddress> addresses) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		addresses.clear();
		//
		HashSet<EmailAddress> set = new HashSet<EmailAddress>();
		ca.loadEmailAddresses(ctx, set);
		addresses.addAll(set);
		//
		sort(addresses);
	}

	private static void sort(List<EmailAddress> addresses) {
		Comparator<EmailAddress> cp = new Comparator<EmailAddress>() {
			public int compare(EmailAddress a1, EmailAddress a2) {
				int c = a1.name.compareTo(a2.name);
				if (c == 0) {
					c = a1.address.compareTo(a2.address);
				}
				return c;
			}
		};
		Collections.sort(addresses, cp);
	}

	/**
	 * 
	 * @param ctx
	 * @param name
	 * @return sorted list of email addresses
	 */
	public static String[] getEmail(Context ctx, String name) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		String[] emails = ca.getEmails(ctx, name);
		if (emails == null) {
			return new String[0];
		}
		Arrays.sort(emails); //
		return emails;
	}
	
	public static void loadContacts(Context ctx, List<EmailContact> contacts) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		contacts.clear();
		HashSet<EmailContact> set = new HashSet<EmailContact>();
		//
		ca.loadEmailContacts(ctx, set );
		//
		contacts.addAll(set);
	}

	public static Bitmap loadPhoto(Context ctx, long id, int res) {
		ContactAdapter ca = ContactAdapter.getAdapter();
		return ca.loadPhoto(ctx, id, res);
	}
}